package com.lemon.rpcframe.dao.behavior;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lemon.rpcframe.annotation.ServiceBehavior;
import com.lemon.rpcframe.dao.DBHelper;
import com.lemon.rpcframe.dao.Total;
import com.lemon.rpcframe.dao.contract.ITaskExecutableService;
import com.lemon.rpcframe.dao.entity.TaskExecutableBean;
import com.lemon.rpcframe.util.DateUtil;

@ServiceBehavior
public class TaskExecutableServiceImpl implements ITaskExecutableService {
    public static final int DEFAULT_PAGESIZE = 30;

    @Override
    public long insert(TaskExecutableBean entity) throws Exception {
        if (null == entity) {
            return 0l;
        }
        long id = 0l;
        Object obj = DBHelper.getInstance().getdaoHelper().sql.insert(entity);
        if (null != obj) {
            id = (long) obj;
        }
        return id;
    }

    @Override
    public List<TaskExecutableBean> loadByPage(long taskid, Date start, Date end, int pageSize, int page, Total<Integer> total) throws Exception {
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 0 || pageSize > 60) {
            pageSize = DEFAULT_PAGESIZE;
        }
        if (taskid < 1) {
            return null;
        }

        int offset = pageSize * (page - 1);

        StringBuffer sbSql = new StringBuffer(60);
        List<Object> objs = new ArrayList<Object>();
        sbSql.append(" WHERE taskid = ? ");
        objs.add(taskid);
        if (start != null) {
            sbSql.append(" and createDate > ?");
            objs.add(DateUtil.fullsdf.format(start));
        }

        if (end != null) {
            sbSql.append(" and createDate < ?");
            objs.add(DateUtil.fullsdf.format(end));
        }

        sbSql.append(" ORDER BY createDate desc");

        sbSql.append(" LIMIT ");
        sbSql.append(offset);
        sbSql.append(",");
        sbSql.append(pageSize);

        total.setTotal(countByTaskId(taskid, start, end));

        List<TaskExecutableBean> list = DBHelper.getInstance().getdaoHelper().sql.getListBySQL(TaskExecutableBean.class, sbSql.toString(), objs.toArray());
        
        return list;
    }

    @Override
    public int countByTaskId(long taskid, Date start, Date end) throws Exception {
        StringBuffer sbSql = new StringBuffer(100);
        List<Object> objs = new ArrayList<Object>();
        sbSql.append(" WHERE taskid = ? ");
        objs.add(taskid);
        if (start != null) {
            sbSql.append(" and createDate > ?");
            objs.add(DateUtil.fullsdf.format(start));
        }
        if (end != null) {
            sbSql.append(" and createDate < ?");
            objs.add(DateUtil.fullsdf.format(end));
        }
        int sum = DBHelper.getInstance().getdaoHelper().sql.getCountBySQL(sbSql.toString(), objs.toArray());

        return sum;
    }

    @SuppressWarnings("unchecked")
    @Override
    public TaskExecutableBean loadOneByGroupRunningTrigger(String taskGroup, boolean running, Date nexttrigDate) throws Exception {
        String where = String.format(" taskGroup ='%s' and running = %d and triggerDate < '%s' ", taskGroup, (running == true) ? 1 : 0, DateUtil.fullsdf.format(nexttrigDate));
        String order = " triggerDate asc ,priority asc limit 1";
        List<TaskExecutableBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(TaskExecutableBean.class, "", where, order);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateByJobIDRunning(long jobId, boolean running, String taskTrackerIdentity, Date modifyDate) throws Exception {
        String where = String.format(" running = %d and jobId = %d ", (running == true) ? 1 : 0, jobId);
        String update = String.format(" taskIdentity='%s' , modifyDate='%s' ,running=%s ", taskTrackerIdentity, DateUtil.fullsdf.format(modifyDate), true);
        DBHelper.getInstance().getdaoHelper().sql.updateByCustom(TaskExecutableBean.class, update, where);
        return 1;
    }

    @Override
    public int updateByJobID(long jobId, String taskTrackerIdentity, Date modifyDate, boolean running) throws Exception {
        String where = String.format(" jobId = %d ", jobId);
        String update = String.format(" taskIdentity='%s' , modifyDate='%s' ,running=%d ", taskTrackerIdentity, DateUtil.fullsdf.format(modifyDate), (running == true) ? 1 : 0);
        DBHelper.getInstance().getdaoHelper().sql.updateByCustom(TaskExecutableBean.class, update, where);
        return 1;
    }

    @Override
    public void deleteByJobID(long jobid, String taskTrackerNodeGroup) throws Exception {
        if (jobid < 1) {
            return;
        }
        String where = String.format(" jobid = %d limit 1", jobid);
        DBHelper.getInstance().getdaoHelper().sql.deleteByCustom(TaskExecutableBean.class, where);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskExecutableBean> loadByModifyRunning(boolean isRunning, Date modifyDate) throws Exception {
        String where = String.format(" running = %d and modifyDate < '%s' ", (isRunning == true) ? 1 : 0, DateUtil.fullsdf.format(modifyDate));
        List<TaskExecutableBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(TaskExecutableBean.class, "", where, "");
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskExecutableBean> loadByPage(int pageSize, int page, Total<Integer> total) throws Exception {
        total.setTotal(countAll());
        List<TaskExecutableBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByPage(TaskExecutableBean.class, "", "", page, pageSize, " triggerDate desc");
        return list;
    }

    @Override
    public int countAll() throws Exception {
        int cnt = DBHelper.getInstance().getdaoHelper().sql.getCount(TaskExecutableBean.class, "");
        return cnt;
    }
}
